//
//  XZNewsHeadView.m
//  Teecloud
//
//  Created by PC on 2018/10/16.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZNewsHeadView.h"
#import "XZNewsBanner.h"

@interface XZNewsHeadView ()<SDCycleScrollViewDelegate>
@property (nonatomic, strong) SDCycleScrollView *bannerView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end
@implementation XZNewsHeadView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
    [self setupBannerView];
    }
    return self;
}
- (void)setupBannerView{
    SDCycleScrollView *bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 180) delegate:self placeholderImage:nil];
    bannerView.bannerImageViewContentMode = UIViewContentModeScaleToFill;
    bannerView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    bannerView.autoScrollTimeInterval = 5;
    [self addSubview:bannerView];
    self.bannerView = bannerView;
}


- (void)setModel:(XZNewsBanner *)model{
    _model = model;
    //移除上一次添加的数据
    [self.dataSource removeAllObjects];
    [self.dataSource addObjectsFromArray:model.Tops];
    
    //图片跟文本数组
    NSMutableArray *imgArray = [[NSMutableArray alloc]init];
    NSMutableArray *titleArray = [[NSMutableArray alloc]init];
    for (XZBannerTops *tops in model.Tops) {
        [imgArray addObject:tops.HeadUrl];
        [titleArray addObject:tops.Title];
    }
    self.bannerView.imageURLStringsGroup = imgArray;
    self.bannerView.titlesGroup = titleArray;
}


#pragma mark - SDCycleScrollViewDelegate
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    XZBannerTops *model = self.dataSource[index];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickNewsBannerIndex:)]) {
        [self.delegate clickNewsBannerIndex:model];
    }
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}

@end
